package cn.lzd.mr.TycMember;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.*;
import java.util.*;

/**
 * Created by lzd on 2018/7/21.
 */
public class MemberClean {
    static final String tagf = "f-";
    static final String tagl = "l-";

    public static class MyMapper extends Mapper<Object, Text, Text, Text> {
        String fileName = null;
        Text wkey = new Text();
        Text wval = new Text();

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            FileSplit fileSplit = (FileSplit) context.getInputSplit();
            fileName = fileSplit.getPath().getName();
        }

        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String str = value.toString();
            wkey.set(str);
            String val = "fileName".equals(fileName) ? tagf : tagl;
            wkey.set(str);
            wval.set(val + str);
            context.write(wkey, wval);

        }
    }

    public static class MyReduce extends Reducer<Text, Text, Text, NullWritable> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

            for (Text value : values) {
                if (value.toString().indexOf(tagf) != -1) {
                    return;
                }
            }

            context.write(key, NullWritable.get());
        }
    }

    public static void main1(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        job.setJarByClass(MemberClean.class);

        job.setMapperClass(MemberClean.MyMapper.class);
        job.setReducerClass(MemberClean.MyReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job, new Path("/G:/log/res/*"));
        FileOutputFormat.setOutputPath(job, new Path("/G:/log/res/out/"));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

    public static void handlerFileName() throws IOException {
        File file = new File("G:/member");
        String[] strs = file.list();
        List<String> list = Arrays.asList(strs);
        FileUtils.writeLines(new File("G:/log/fileName"), list);
    }

    public static void HandlerLog() throws IOException {
        Set<String> sets = new HashSet<>();
        List<String> list = FileUtils.readLines(new File("G:/log/log1.log"));
        list.forEach((str) -> {
            if (str.indexOf("。。error。。") != -1) {
                int lastIndex = str.indexOf("。");
                String pageCode = str.substring(str.indexOf("mans/") + 5, lastIndex);
                pageCode = pageCode.replace("/", ".");
                sets.add(pageCode);
            }
        });

        List<String> list2 = FileUtils.readLines(new File("G:/log/log2.log"));
        list2.forEach((str) -> {
            if (str.indexOf("。。error。。") != -1) {
                int lastIndex = str.indexOf("。");
                String pageCode = str.substring(str.indexOf("mans/") + 5, lastIndex);
                pageCode = pageCode.replace("/", ".");
                sets.add(pageCode);
            }
        });

        FileUtils.writeLines(new File("G:/log/log3.log"), sets);
    }

    public static void main2(String[] args) throws IOException {
        Set<String> sets = new HashSet<>();
        List<String> list = FileUtils.readLines(new File("G:/spider.log"));

        list.forEach((str) -> {
            if (str.indexOf("。。error。。") != -1) {
                int lastIndex = str.indexOf("。");
                String pageCode = str.substring(str.indexOf("mans/") + 5, lastIndex);
                sets.add(pageCode);
            }
        });
        FileUtils.writeLines(new File("G:/aaaa.log"), sets);

    }

    public static void main(String[] args) throws IOException {
        File file = new File("G:/tyc_member/member");

        File[] files = file.listFiles();
        File outfile = new File("G:/tyc_member/tycMember");
        for (File f : files) {
            System.out.println(f.getPath());
            List<String> list = FileUtils.readLines(f);
            FileUtils.writeLines(outfile, list, true);
        }
    }
}
